#/usr/bin/python
#wirte the java automatic import
import re
class JavaTable:
    def __init__(self,package,domain):
        #table:java id,java type
        self.table=[]
        self.domain=domain
        self.package=package
    def add_list(self,javaid,javatype):
        java_setId=javaid[0].upper()+javaid[1:]
        id_colum=(javaid,javatype,java_setId)
        self.table.append(id_colum)
    def __to_java_domain(self):
        sentences=""
        for column in self.table:
            sentences=sentences+"private "+column[1]+" "+column[0]+";\n\n"
        return sentences
    def __to_java_get(self):
        #public ItemId setItemId()
        #{
        #    this.ItemId=ItemId;    
        #}
        sentences=""
        for column in self.table:
            types=column[1]
            id=column[0]
            java_setId=column[2]
            def_get_sentences="public void"+" set"+java_setId+"("+types+" "+id+")"
            def_get_func="{"+"this."+id+"="+id+";}"
            sentences=sentences+def_get_sentences+def_get_func+"\n\n";
        return sentences
    def __to_java_import(self):
        import_table={
                'Timestamp':'import java.sql.timeStamp;',
                 self.domain:'import '+self.package+".domain;"
                }
        sentence=""
        for column in self.table:
            type=column[1]
            import_str=import_table.get(type)
            if import_str is not None:
                sentence=sentence+import_str+"\n"
        return sentence
    def __to_java_set(self):
        #public ItemId setItemId()
        #{
        #    this.ItemId=ItemId;    
        #}
        sentences=""
        for column in self.table:
            id=column[0]
            types=column[1]
            java_setId=column[2]
            def_get_sentences="public "+types+" get"+java_setId+"()"
            def_get_func="{ return"+" this."+id+";}"
            sentences=sentences+def_get_sentences+def_get_func+"\n\n";
        return sentences

    def toJavaFile(self):
        package_sentences="package "+self.package+"\n\n";
        package_sentences=package_sentences+self.__to_java_import()+"\n\n";
        class_sentences=package_sentences+"class "+self.domain+"{\n\n"
        class_sentences=class_sentences+self.__to_java_domain()+self.__to_java_get()+self.__to_java_set()
        class_sentences=class_sentences+"}"
        return class_sentences;


class JavaParser:
    def parser(self,sentences):
        for sentence in sentences.split("|"):
            sentence=re.sub("\s+$","",sentence)
            ids=sentence.split(",")[0]
            ids=re.split("\s",ids)
            yield (ids[0],ids[1]) 
    def run_parser(self,packagename,domain,sentences):
        table=self.get_java_table(packagename,domain,sentences)
        return table.toJavaFile()
    def get_java_table(self,packagename,domain,sentences):
        table=JavaTable(packagename,domain)
        for sentence in self.parser(sentences):
                table.add_list(sentence[0],sentence[1])
        return table
        
        

parser_file="pictureUrl String,varchar(20)|setTime Timestamp|sid Long"
if __name__=="__main__":
    parse=JavaParser()
    print(parse.run_parser("com.trendata.function","SelectItem",parser_file))

